package de.blitzcoder.cddatabase.gui.cdtable;

import de.blitzcoder.cddatabase.data.CD;
import de.blitzcoder.cddatabase.data.Data;
import de.blitzcoder.cddatabase.gui.EditCDDialog;
import de.blitzcoder.cddatabase.gui.datatable.DataTable;
import java.awt.Frame;
import java.util.ListIterator;

/**
 *
 * @author blitzcoder
 */
public class CDTable extends DataTable {

    private static final String[]   COLUMNS = new String[] {"ID", "Name", "Künstler", "Jahr"};
    private static final int[]      COLUMNWIDTHS = new int[] {40,0,0,50};

    public CDTable() {
        super(COLUMNS,COLUMNWIDTHS);
        updateCDList();
    }

    public void updateCDList() {
        getModel().clear();
        CDTableRow[] rows = new CDTableRow[Data.cdList.size()];
        ListIterator<CD> it = Data.cdList.listIterator();
        int i = 0;
        while (it.hasNext()) {
            rows[i] = new CDTableRow(it.next());
            i++;
        }
        getModel().addRows(rows);
    }

    public boolean editCurrent(Frame parent) {
        int r = getSelectedRow();
        if (r == -1)
            return false;

        CDTableRow row = (CDTableRow) getModel().getRow(r);

        EditCDDialog dialog = new EditCDDialog(parent, row.getCD());
        dialog.setVisible(true);

        return true;
    }

}
